package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.modules.sxjy.common.noresubmit.CacheLock;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeLog;
import org.edu.modules.sxjy.service.CacheKeyGenerator;
import org.edu.modules.sxjy.service.IVeLogService;
import org.edu.modules.sxjy.service.impl.CacheKeyGeneratorImp;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.vo.VeLogInfoVo;
import org.edu.modules.sxjy.vo.VeLogShowCpPostVo;
import org.edu.modules.sxjy.vo.VeLogVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"学生实习记录"})
@RestController
@RequestMapping({"/sxjy/veLog"})
@ApiSort(60)
public class VeLogController
        extends BaseController<VeLog, IVeLogService>
{
    private static final Logger log = LoggerFactory.getLogger(VeLogController.class);
    @Autowired
    private IVeLogService veLogService;

    @Bean
    public CacheKeyGenerator cacheKeyGenerator()
    {
        return new CacheKeyGeneratorImp();
    }

    @AutoLog("学生实习记录-学生实习列表")
    @ApiOperation(value="学生实习列表", notes="学生实习记录-查询实习记录")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectVeLog(VeLogVo veLogVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, VeCommon veCommon)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VeLogVo> veLogList = this.veLogService.selectPraLog(veLogVo, startLine, pageSize, veCommon);
        int total = this.veLogService.getSumPage(veLogVo, veCommon);
        if ((Objects.isNull(veLogList)) || (veLogList.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
        }
        double totals = total;
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total, Result.OK(veLogList));
    }

    @AutoLog("学生实习记录-批量删除")
    @ApiOperation(value="批量删除", notes="学生实习记录-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veLogService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @AutoLog("学生实习记录-查看详细信息")
    @ApiOperation(value="查看详细信息", notes="学生实习记录-查看实习记录")
    @GetMapping({"/selectOneInfo"})
    public Result<?> selectOneInfo(int id)
    {
        VeLogInfoVo veChangeInfoVo = this.veLogService.getOneInfo(id);
        if (veChangeInfoVo == null) {
            return Result.OK();
        }
        return Result.OK(veChangeInfoVo);
    }

    @AutoLog("学生实习记录-新增信息")
    @ApiOperation(value="新增信息", notes="学生实习记录-新增信息")
    @PostMapping({"/add"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> add(@RequestBody VeLogInfoVo veLogInfoVo)
    {
        if (veLogInfoVo != null)
        {
            this.veLogService.saves(veLogInfoVo);
            return Result.OK("添加成功！");
        }
        return Result.error("添加失败");
    }

    @AutoLog("学生实习记录-显示学生企业与岗位信息")
    @ApiOperation(value="显示学生企业与岗位信息", notes="学生实习记录-显示学生企业与岗位信息")
    @GetMapping({"/showCpPostName"})
    public Result<?> showCpPostName(String userId)
    {
        VeLogShowCpPostVo veLogShowCpPostVo = this.veLogService.showCpPostName(userId);
        if (veLogShowCpPostVo == null) {
            return Result.OK();
        }
        return Result.OK(veLogShowCpPostVo);
    }

    @AutoLog("学生实习记录-编辑回显")
    @ApiOperation(value="编辑回显", notes="学生实习记录-编辑回显")
    @GetMapping({"/udpateEcho"})
    public Result<?> updateEcho(int id)
    {
        if (id != 0)
        {
            VeLogInfoVo veLogInfoVo = this.veLogService.editEcho(id);
            return Result.OK(veLogInfoVo);
        }
        return Result.error("编辑失败");
    }

    @AutoLog("学生实习记录-编辑")
    @ApiOperation(value="编辑", notes="学生实习记录-编辑")
    @PutMapping({"/edit"})
    @CacheLock(prefix="redisLock.test", expire=1)
    public Result<?> edit(@RequestBody VeLogInfoVo veLogInfoVo)
    {
        if (veLogInfoVo != null)
        {
            this.veLogService.edit(veLogInfoVo);
            return Result.OK("编辑成功！");
        }
        return Result.OK("编辑成功!");
    }
}
